﻿@model Citi.Scheduler.WebUi.Models.RoutineModel
<script type="text/javascript">
    $(function () {
        if ($('#Id').length == 0) {
            $("#ResponsibleId").val('@(((Citi.Scheduler.Domain.User)Session["LoggedUser"]).Id)');
        }
        else if ($("#ResponsibleId").val() == '@(((Citi.Scheduler.Domain.User)Session["LoggedUser"]).Id)') {
            $("#ddlResponsibleId").removeAttr('disabled');
        }

        $("#ddlResponsibleId").val($("#ResponsibleId").val());

        $("#ddlResponsibleId").change(function () {
            $("#ResponsibleId").val($("#ddlResponsibleId").val());    
        });

        $("#Type").change(loadLibraries);
        $("#LibraryId").change(loadVersions);
        //$("#CategoryId").change(loadLibraries);
        $("#LibraryVersionId").change(loadClasses);
        $("#PredecessorLibraryId").change(loadRoutinesPredecessor);

        $('#Class').removeAttr('multiple');

        if ($("#TimesToTryAgain").val() == null)
            $("#TimesToTryAgain").val('0');

        if ($("#TryAgainAfterMinutes").val() == null)
            $("#TryAgainAfterMinutes").val('0');
    });

    function loadRoutinesPredecessor() {
        var predecessorRoutine = $('#PredecessorRoutineId');
        predecessorRoutine.empty();

        if ($("#PredecessorLibraryId :selected").val() == '') return;

        $.getJSON('@Url.Action("GetRoutinesByLibrary")', { libraryId: $("#PredecessorLibraryId :selected").val(), type: $("#Type :selected").val(), id: $("#Id").val() },
            function (data) {
                $.each(data, function (i, option) {
                    predecessorRoutine.append($('<option />').attr('value', option.Id).text(option.Name));
                });
            });
    }

    function loadVersions() {
        clearVersionDropDown();
        clearClassList();

        if ($("#LibraryId :selected").val() == '') return;

        $.getJSON('@Url.Action("GetVersion")', { libraryId: $("#LibraryId :selected").val(), type: $("#Type :selected").val() },
            function (data) {
                $("#LibraryVersionId").empty();
                $.each(data, function (i, option) {
                    $("#LibraryVersionId").append($('<option />').attr('value', option.Id).text(option.Version));
                });

                loadClasses();
            });
    }

    //    function loadCategories() {
    //        clearCategoryDropDown();
    //        clearLibraryDropDown();
    //        clearVersionDropDown();
    //        clearClassList();

    //        $.getJSON('@Url.Action("GetCategoriesByType")', { type: $("#Type :selected").val() },
    //            function (data) {
    //                $("#CategoryId").empty();
    //                $.each(data, function (i, option) {
    //                    $("#CategoryId").append($('<option />').attr('value', option.Id).text(option.Name));
    //                });
    //            });

    //        loadPredecessorLibraries();
    //    }

    function loadPredecessorLibraries() {
        $.getJSON('@Url.Action("GetLibrariesByType")', { type: $("#Type :selected").val() },
            function (data) {
                $("#PredecessorLibraryId").empty();
                $.each(data, function (i, option) {
                    $("#PredecessorLibraryId").append($('<option />').attr('value', option.Id).text(option.Name));
                });
            });
    }

    function loadLibraries() {
        clearLibraryDropDown();
        clearPredecessorLibraryDropDown();
        clearVersionDropDown();
        clearClassList();

        //$.getJSON('@Url.Action("GetLibrariesByCategoryAndType")', { categoryId: $("#CategoryId :selected").val(), type: $("#Type :selected").val() },
        $.getJSON('@Url.Action("GetLibrariesByType")', { type: $("#Type :selected").val() },
            function (data) {
                $("#LibraryId").empty();
                $.each(data, function (i, option) {
                    $("#LibraryId").append($('<option />').attr('value', option.Id).text(option.Name));
                });
            });

        loadPredecessorLibraries();
    }

    function loadClasses() {
        //if ($("#Libraries :selected").val() == '') return;
        clearClassList();

        var libraryVersionId = $("#LibraryVersionId :selected").val();

        if (libraryVersionId == "-1") libraryVersionId = $("#LibraryVersionId :nth-child(2)").val();

        $.getJSON('@Url.Action("GetClass")', { libraryVersionId: libraryVersionId },
            function (data) {
                $("#Class").empty();
                $.each(data, function (i, option) {
                    $("#Class").append($('<option />').attr('value', option.ClassName).text(option.ClassName));
                });
            });
    }

    function clearPredecessorLibraryDropDown() {
        var library = $("#PredecessorLibraryId").empty();
        library.empty();
        library.append($('<option />').attr('value', '-1').text('Select a Library'));
    }

    function clearLibraryDropDown() {
        var library = $("#LibraryId").empty();
        library.empty();
        library.append($('<option />').attr('value', '-1').text('Select a Library'));
    }

    function clearCategoryDropDown() {
        var category = $("#CategoryId").empty();
        category.empty();
        category.append($('<option />').attr('value', '-1').text('Select a Type'));
    }

    function clearClassList() {
        var classLibrary = $("#Class").empty();
        classLibrary.empty();
        classLibrary.append($('<option />').attr('value', '-1').text('Select a library'));
    }

    function clearVersionDropDown() {
        var libraryDropDown = $("#LibraryVersionId");
        libraryDropDown.empty();
        libraryDropDown.append($('<option />').attr('value', '-2').text('Select a library'));
    }

    //    function closeDialogRoutine() {
    //        $("#divUpdateRoutine").dialog('close');
    //        clearForm();
    //    }

    function clearForm() {
        $("#formActionForm").trigger('reset');
    }
</script>
@*@using (Ajax.BeginForm("ActionForm", "Routine", new AjaxOptions
{
    UpdateTargetId = "divIndexRoutine",
    InsertionMode = InsertionMode.Replace
}, new { id = "formActionForm" }))
{*@
@using (Html.BeginForm("ActionForm", "Routine", FormMethod.Post, new { id = "formActionForm" }))
{
    <ul class="nav nav-tabs">
        <li class="active"><a href="#basic" data-toggle="tab">Basic Options</a></li>
        <li><a href="#advanced" data-toggle="tab">Advanced Options</a></li>
        <li><a href="#errorManagement" data-toggle="tab">Errors Management</a></li>
        <li><a href="#customerSla" data-toggle="tab">SLA</a></li>
    </ul>
    <div class="tab-content" style="width: 98%; overflow: hidden;">
        <div class="tab-pane active" id="basic">
            <div style="float: left; margin-right: 20px; width: 100%">
                Name:&nbsp;&nbsp;@Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Responsible:&nbsp;&nbsp;
                @Html.DropDownList("ddlResponsibleId", (IEnumerable<SelectListItem>)ViewBag.Responsibles, new { disabled = "disabled" })
                @Html.HiddenFor(model => model.ResponsibleId)
            </div>
            <div class="clear">
                <br />
            </div>
            <div style="float: left; margin-right: 20px; width: 100%;">
                Type:&nbsp;&nbsp;
                @Html.DropDownListFor(model => model.Type, (IEnumerable<SelectListItem>)ViewBag.Types)
                &nbsp;&nbsp;&nbsp;Category:&nbsp;&nbsp;
                @Html.DropDownListFor(model => model.CategoryId, (IEnumerable<SelectListItem>)ViewBag.Categories)
            </div>
            <div class="clear">
                <br />
            </div>
            <div style="float: left; margin-right: 20px; width: 100%;">
                Library:&nbsp;&nbsp;
                @Html.DropDownListFor(model => model.LibraryId, (IEnumerable<SelectListItem>)ViewBag.Libraries)
                @if (Model != null && Model.UseLastVersion) { Model.LibraryVersionId = -1; }
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version:&nbsp;&nbsp;
                @Html.DropDownListFor(model => model.LibraryVersionId, (IEnumerable<SelectListItem>)ViewBag.Versions)
            </div>
            <div class="clear">
                <br />
            </div>
            <div style="float: left; margin-right: 20px; width: 100%;">
                Estimated time: &nbsp;&nbsp;
                @Html.TextBoxFor(model => model.EstimatedTime, new { style = "width:80px", @class = "integer" })
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Percentage of Acceptable Variance: &nbsp;&nbsp;
                @Html.TextBoxFor(model => model.PercentageAcceptableVariation, new { style = "width:80px", @class = "integer" })
            </div>
            <div class="clear">
                <br />
            </div>
            <div style="float: left;">
                @Html.LabelFor(model => model.Class)
                @Html.DropDownListFor(model => model.Class, (IEnumerable<SelectListItem>)ViewBag.Classes, new { style = "height:80px;width:350px;", size = "5" })
            </div>
            <div style="clear: both;">
            </div>
        </div>
        <div class="tab-pane" id="advanced">
            <div id="divAdvancedOptions">@* style="display: @(Model != null && (Model.PredecessorRoutineId != null || Model.PreferredMachineId != null) ? "block" : "none");">*@
                <input type="checkbox" onclick="$('#divPredecessor').toggle();  $('#PredecessorRoutineId').val('-1');" @(Model != null && Model.PredecessorRoutineId != null ? "checked" : string.Empty) />
                Use predecessor routine
                <div id="divPredecessor" style="display: @(Model != null && Model.PredecessorRoutineId != null ? "block" : "none");">
                    <div class="floatLeft" style="margin-right: 50px;">
                        <div class="editor-field">
                            Library:
                            @Html.DropDownListFor(model => model.PredecessorLibraryId, (IEnumerable<SelectListItem>)ViewBag.PredecessorLibraries)
                        </div>
                    </div>
                    <div class="floatLeft">
                        <div class="editor-field">
                            Routine:
                            @Html.DropDownListFor(model => model.PredecessorRoutineId, (IEnumerable<SelectListItem>)ViewBag.Predecessors)
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
                <div>
                    <div class="editor-label">
                        <input type="checkbox" onclick="$('#divMachine').toggle(); $('#PreferredMachineId').val(''); " @(Model != null && Model.PreferredMachineId != null ? "checked" : string.Empty) />
                        Use only a machine
                    </div>
                    <div class="editor-field" id="divMachine" style="display: @(Model != null && Model.PreferredMachineId != null ? "block" : "none");">
                        Machine: @Html.DropDownListFor(model => model.PreferredMachineId, (IEnumerable<SelectListItem>)ViewBag.Machines)
                    </div>
                </div>
            </div>
        </div>
        <div class="tab-pane" id="errorManagement">
            <div style="width: 100%;">
                Try Again After &nbsp;&nbsp;@Html.TextBoxFor(model => model.TryAgainAfterMinutes, new { style = "width:80px", @class = "integer" })&nbsp;&nbsp;
                minutes.
                <br />
                Try Again &nbsp;&nbsp;@Html.TextBoxFor(model => model.TimesToTryAgain, new { style = "width:80px", @class = "integer" })&nbsp;&nbsp;
                times.
            </div>
        </div>
        <div class="tab-pane" id="customerSla">
            <div style="width: 100%;">
                Customer SLA: @Html.TextBoxFor(model => model.CustomerSLA, new { style = "width:80px", @class = "time" })
                <br />
                Warning &nbsp;&nbsp;@Html.TextBoxFor(model => model.MinutesWarningSLA, new { style = "width:80px", @class = "integer" })&nbsp;&nbsp;
                minutes before Customer SLA
                <br />
                Critical Warning &nbsp;&nbsp;@Html.TextBoxFor(model => model.MinutesCriticalSLA, new { style = "width:80px", @class = "integer" })&nbsp;&nbsp;
                minutes before Customer SLA
                <br />
            </div>
        </div>
    </div>

    
@*@using (Html.BeginForm("ActionForm", "Routine", FormMethod.Post, new { id = "formActionForm", enctype = "multipart/form-data" }))
{*@
    @Html.ValidationSummary(true)
    
    
    <div class="clear">
    </div>
@*if (Model != null)
    {
    <script type="text/javascript">
        loadClasses();
    </script>
    }*@
    
@*<br />
    <a href="#" onclick="$('#divAdvancedOptions').toggle()">+ Advanced Options</a>
    <br />*@
    
    <div style="clear: both;">
    </div>
                if (Model != null)
                {
    @Html.HiddenFor(model => model.Id)
                }
    <p>
        <input type="submit" value="Save" id="saveRoutine" class="hide" />
    </p>
}
